package cn.wbnyua.resume.core.security;

import cn.wbnyua.resume.util.JsonResponse;
import com.alibaba.fastjson2.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者：wbnyua.
 * 创建时间: 2023/2/13 11:23.
 */

@Component
public class MyLoginFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");

        JsonResponse<Object> jsonResponse = new JsonResponse<>("50500", "登录失败");

        String result = JSON.toJSONString(jsonResponse);

        response.getWriter().write(result);
    }
}
